package com.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * 定义线程池
 */
@Configuration
public class ThreadPoolConfig1 {


    /**
     * 定义线程池
     *
     * @return
     */
    @Bean("dataDrawExecutor")
    public ThreadPoolTaskExecutor dataCollectionExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 核心线程数量：当前机器的核心数
        executor.setCorePoolSize(Runtime.getRuntime().availableProcessors());
        // 最大线程数
        executor.setMaxPoolSize(Runtime.getRuntime().availableProcessors() * 2);
        // 队列大小
        executor.setQueueCapacity(Integer.MAX_VALUE);
        // 线程池中的线程名前缀
        executor.setThreadNamePrefix("draw-");
        // 拒绝策略：
        executor.setRejectedExecutionHandler(
                new ThreadPoolExecutor.CallerRunsPolicy());
        // 执行初始化
        executor.initialize();
        return executor;
    }

}




